<template>
    <a-modal :width="550"
             :title="title"
             destroyOnClose
             :maskClosable="false"
             v-model="visible">
        <a-form-model
                ref="ruleForm"
                small
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol">
            <a-form-model-item label="菜单名称" prop="resourceName">
                <a-input placeholder="请输入菜单名称" v-model="form.resourceName" />
            </a-form-model-item>
            <a-form-model-item label="链接地址" prop="resourceUrl">
                <a-input placeholder="请输入链接地址" v-model="form.resourceUrl" />
            </a-form-model-item>
            <a-form-model-item label="图标资源" prop="resourceIcon">
                <a-input placeholder="请输入图标资源" v-model="form.resourceIcon" />
            </a-form-model-item>
            <a-form-model-item label="菜单排序" prop="sortBy">
                <a-input-number class="width-percent-100" :precision="0" placeholder="请输入菜单排序" v-model="form.sortBy" />
            </a-form-model-item>
            <a-form-model-item label="是否隐藏" prop="hideFlag">
                <a-radio-group v-model="form.hideFlag">
                    <a-radio :value="0">显示</a-radio>
                    <a-radio :value="1">隐藏</a-radio>
                </a-radio-group>
            </a-form-model-item>
        </a-form-model>
        <div slot="footer" class="text-c">
            <a-button @click="close">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="sure">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    export default {
        data () {
            return {
                form: {
                    resourceName: '',
                    resourceUrl: '',
                    resourceIcon: '',
                    sortBy: '',
                    hideFlag: 0,
                    parentId: '',
                    resourceType: 1,
                    id: '',
                },
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
                rules: {
                    resourceName: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' },
                    ],
                    resourceUrl: [
                        { required: true, message: '请输入链接地址', trigger: 'blur' },
                    ],
                    sortBy: [
                        { required: true, message: '请输入排序', trigger: 'blur' },
                    ]
                },
                visible: false,
                title: '新增菜单',
                actionType: null,
                loading: false,
            };
        },
        watch: {
            visible (val) {
                if (!val) {
                    this.close();
                }
            }
        },
        methods: {
            sure () {
                this.$refs.ruleForm.validate(valid => {
                    if (valid) {
                        let api = this.actionType === 0 ? this.$api.permission.addPermission : this.$api.permission.editPermission;
                        let tipMsg = this.actionType === 0 ? '新增成功' : '编辑成功';
                        this.editPermissionMenu(api,this.form,tipMsg);
                    }
                });
            },
            editPermissionMenu (api,data,tipMsg) {
                api(data).then(res => {
                    if (res && res.code !== 200) {
                        this.$message.error(res.errorMsg);
                        return;
                    }
                    this.$message.success(tipMsg);
                    this.$emit('done');
                    this.close();
                }).catch(err => {
                    this.$message.error(err.errorMsg);
                });
            },
            show (data,title,actionType) {
                this.form = {
                    resourceName: data.resourceName || '',
                    resourceUrl: data.resourceUrl || '',
                    resourceIcon: data.resourceIcon || '',
                    sortBy: data.sortBy || '',
                    hideFlag: data.hideFlag || 0,
                    parentId: data.parentId || '',
                    id: data.id || '',
                };
                this.actionType = actionType;
                this.title = title;
                this.visible = true;
            },
            close () {
                this.form = {
                    resourceName: '',
                    resourceUrl: '',
                    resourceIcon: '',
                    sortBy: '',
                    hideFlag: 0,
                    parentId: '',
                    id: '',
                    resourceType: 1,
                };
                this.title = '';
                this.actionType = null;
                this.visible = false;
            },
        }
    };
</script>

<style scoped lang="scss">

</style>
